<%-- 
    Document   : index
    Created on : Apr 15, 2013, 11:28:05 PM
    Author     : Lord Makuta
--%>

<%@page import="db.UserDAO"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page import="db.MovieObj"%>
<%@page import="db.DB"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.util.Vector"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>DataFlix</title>
	
	<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<meta http-equiv="content-language" content="en" />
	<meta name="description" content="" />
	<meta name="language" content="en" />
	
	<link type="text/css" rel="stylesheet" href="./styles/fonts.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/header.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/fancybox.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/images.css"/>
	
	<link type="text/css" rel="stylesheet" href="./styles/charcoal.css"/>
	<link type="text/css" rel="stylesheet" href="./styles/style.css"/>
	
	<link rel="shortcut icon" href="http://www.iconarchive.com/download/i59247/franksouza183/fs/Apps-firefox.ico"/>
	
</head>
<body>
       
	<jsp:include page="./header.jsp" />
	
        <%//generate movies for main page
            //int[][] ids=new int[6][6];
            MovieObj[][] movies=new MovieObj[6][6];
            ArrayList<MovieObj> alGen=new ArrayList<MovieObj>();
            //Vector<MovieObj> vecGen=new Vector<MovieObj>();
            String[] rows={"Comedy", "Drama", "Action", "Foreign", "Best", "Reco"};
            
            
            DB data=new DB();
            
            Connection conn=null;
            Statement stm=null;
            ResultSet res=null;
            MovieObj tmp=null;
            try
            {
                conn=data.getConnection();
                
                for (int m=0; m<4; m++)         //random movies for genres
                {
                    stm=conn.createStatement();
                    res=stm.executeQuery("SELECT Id, Name, Type FROM Movie WHERE Type='"+rows[m]+"'");
                    
                    tmp=new MovieObj();
                    while (res.next())
                    {
                        //alGen.add(res.getInt(1));
                        tmp=new MovieObj(res.getInt(1), res.getString(2), res.getString(3));
                        alGen.add(tmp);
                    }

                    for (int i=0; i<6; i++)
                    {
                        /*int index=(int)(Math.random()*alGen.size());
                        ids[m][i]=alGen.get(index);
                        alGen.remove(index);*/
                        
                        int index=(int)(Math.random()*alGen.size());
                        movies[m][i]=alGen.get(index);
                        alGen.remove(index);
                    }
                    alGen.clear();
                }
                
                //best selling movies
                stm=conn.createStatement();
                res=stm.executeQuery("SELECT batch.Id, batch.Name, batch.Type FROM (SELECT COUNT(R.MovieId) AS cnt, M.Id, M.Name, M.Type, M.Rating "
                    + "FROM Movie M, Rental R WHERE M.Id=R.MovieId GROUP BY R.MovieId ORDER BY cnt DESC) as batch ORDER BY batch.Rating DESC, batch.cnt DESC");
                int q=0;
                while (res.next())
                {
                    tmp=new MovieObj(res.getInt(1), res.getString(2), res.getString(3));
                    movies[4][q]=tmp;
                    q++;
                    if (q>5)
                    {
                        break;
                    }
                }
                
                //recommendations
                if (session.getAttribute("user")!=null)
                {
                    res=((UserDAO)session.getAttribute("user")).getRecoQ().executeQuery();
                    q=0;
                    while (res.next())
                    {
                        tmp=new MovieObj(res.getInt(1), res.getString(2), res.getString(3));
                        movies[5][q]=tmp;
                        q++;
                        if (q>5)
                        {
                            break;
                        }
                    }
                }
            }
            catch(Exception e)
            {
                
            }
            finally
            {
                try{conn.close();}catch(Exception ee){};
            }
            
        %>
	
	<div id="featured">	
		<h1>Featured Film</h1>		
		<a class="fancyBox" id="movie8" href="./movie.jsp?id=8">
                    <div class="overlay"><h1>Undefined Fantastic Object</h1></div>
		</a>
	</div>
	
	
	<div id="content">
		
            <% String path=null;   File f=null;    FileInputStream fis=null;   InputStreamReader inStream=null;    BufferedReader buff=null;
            if (session.getAttribute("user")!=null) { 
                
            %>
            <ul class="contentRow" id="Reco">
                <li class="rowTtl"><a href="category.jsp?genre=Reco">Recommended
                    <div class="ttlOvr"></div>
                </a></li>
                
                <%  for (int j=0; j<6; j++)
                    {   
                        if (movies[5][j]!=null)
                        {
                %>                       
                    <li <%if (j-3>=0) out.write("class=\"expLef\"");%> ><a class="fancyBox" <%="href=\"./movie.jsp?id="+movies[5][j].getId()+"\""%> >
                            <img <%="id=\"movie"+movies[5][j].getId()+"\""%> <%="style=\"background-image: url(./images/movies/"+movies[5][j].getGenre().toLowerCase()+"/movie"+movies[5][j].getId()+".jpg\""%>/>
                            <div class="overlay">
                                    <h1><%=movies[5][j].getTitle()%></h1>
                                    <div class="shortDesc">
                                            <%
                                                path=application.getRealPath("/");
                                                f=new File(path+"images/movies/"+movies[5][j].getGenre().toLowerCase()+"/movie"+movies[5][j].getId()+"Desc.txt");
                                                fis=new FileInputStream(f);
                                                inStream = new InputStreamReader(fis);
                                                buff = new BufferedReader(inStream);

                                                path=buff.readLine();
                                                out.print(path.substring(0, 205)+"...");
                                            %>
                                    </div>
                            </div>
                    </a></li>
                    <% }}
                        if (movies[5][0]==null)
                            out.print("<h1 style=\"display:inline-block;\">You haven't rented any movies yet, rent some so we can find something you'll like</h1>");
                    %>
            </ul>
            <%}%>
		
            <ul class="contentRow" id="Best">
                    <li class="rowTtl"><a href="category.jsp?genre=Best">Best Selling
                            <div class="ttlOvr"></div>
                    </a></li>

                    <%  
                        for (int j=0; j<6; j++)
                        {   
                            if (movies[4][j]!=null)
                            {
                    %>                       
                    <li <%if (j-3>=0) out.write("class=\"expLef\"");%> ><a class="fancyBox" <%="href=\"./movie.jsp?id="+movies[4][j].getId()+"\""%> >
                            <img <%="id=\"movie"+movies[4][j].getId()+"\""%> <%="style=\"background-image: url(./images/movies/"+movies[4][j].getGenre().toLowerCase()+"/movie"+movies[4][j].getId()+".jpg\""%>/>
                            <div class="overlay">
                                    <h1><%=movies[4][j].getTitle()%></h1>
                                    <div class="shortDesc">
                                            <%
                                                path=application.getRealPath("/");
                                                f=new File(path+"images/movies/"+movies[4][j].getGenre().toLowerCase()+"/movie"+movies[4][j].getId()+"Desc.txt");
                                                fis=new FileInputStream(f);
                                                inStream = new InputStreamReader(fis);
                                                buff = new BufferedReader(inStream);

                                                path=buff.readLine();
                                                out.print(path.substring(0, 205)+"...");
                                            %>
                                    </div>
                            </div>
                    </a></li>
                    <% }} %>
            </ul>
		
		
                                        
            <%
                for (int i=0; i<4; i++)
                {

                %>
                <ul class="contentRow" <%="id=\""+rows[i]+"\""%> >

                    <li class="rowTtl"><a href=<%="category.jsp?genre="+rows[i]%>><%=rows[i]%>
                            <div class="ttlOvr"></div>
                    </a></li> 
                <%
                    for (int j=0; j<6; j++)
                    {   
                    %>                       
                    <li <%if (j-3>=0) out.write("class=\"expLef\"");%> ><a class="fancyBox" <%="href=\"./movie.jsp?id="+movies[i][j].getId()+"\""%> >
                            <img <%="id=\"movie"+movies[i][j].getId()+"\""%> <%="style=\"background-image: url(./images/movies/"+movies[i][j].getGenre().toLowerCase()+"/movie"+movies[i][j].getId()+".jpg\""%>/>
                            <div class="overlay">
                                    <h1><%=movies[i][j].getTitle()%></h1>
                                    <div class="shortDesc">
                                            <%
                                                path=application.getRealPath("/");
                                                f=new File(path+"images/movies/"+movies[i][j].getGenre().toLowerCase()+"/movie"+movies[i][j].getId()+"Desc.txt");
                                                fis=new FileInputStream(f);
                                                inStream = new InputStreamReader(fis);
                                                buff = new BufferedReader(inStream);

                                                path=buff.readLine();
                                                out.print(path.substring(0, 205)+"...");
                                            %>
                                    </div>
                            </div>
                    </a></li>
                    <%
                    }
                        out.write("</ul>");
                }
            %>
		
	</div>
	
	<div id="footer"></div>

</body></html>
